package com.airili.wiki.service.impl;

import com.airili.wiki.domain.Ebook;
import com.airili.wiki.domain.EbookExample;
import com.airili.wiki.mapper.EbookMapper;
import com.airili.wiki.req.EbookQueryReq;
import com.airili.wiki.req.EbookSaveReq;
import com.airili.wiki.resp.EbookQueryResp;
import com.airili.wiki.resp.PageResp;
import com.airili.wiki.service.EbookService;
import com.airili.wiki.util.CopyUtil;
import com.airili.wiki.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author: Airili
 * @create: 2021-09-24 10:12
 **/
@Service
public class EbookServiceImpl implements EbookService {
    private static final Logger LOG = LoggerFactory.getLogger(EbookServiceImpl.class);

    @Resource
    private SnowFlake snowFlake;

    @Resource
    private EbookMapper ebookMapper;

    @Override
    public PageResp<EbookQueryResp> list(EbookQueryReq req) {
        // 以下两行代码为 Example 通用写法格式
        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();

        // 根据名字模糊查询，动态SQL
        if (!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }

        // 根据子分类查询相应的电子书
        if (!ObjectUtils.isEmpty(req.getCategoryId2())) {
            criteria.andCategory2IdEqualTo(req.getCategoryId2());
        }

        // 使用 pagehelper 分页插件，
        // 分页功能 = 查总数 (select count(0) from table) + 查当前页数据 (select xxx from table limit ?)
        // 使用时注意！！！ ： 该分页只对首个遇到的 select 起作用，所以使用的时候应该把该语句放在查询语句的前一行
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Ebook> ebooks = ebookMapper.selectByExample(ebookExample);

        // 使用 pageinfo
        PageInfo<Ebook> pageInfo = new PageInfo<>(ebooks);
        LOG.info("总行数: {}", pageInfo.getTotal());
        LOG.info("总页数: {}", pageInfo.getPages());

        List<EbookQueryResp> ebookQueryResps = CopyUtil.copyList(ebooks, EbookQueryResp.class);

        PageResp<EbookQueryResp> resp = new PageResp<>();
        resp.setTotal(pageInfo.getTotal());
        resp.setList(ebookQueryResps);
        return resp;
    }

    @Override
    public List<EbookQueryResp> all(EbookQueryReq req) {
        List<Ebook> ebooks = ebookMapper.selectByExample(null);
        List<EbookQueryResp> ebookQueryResps = CopyUtil.copyList(ebooks, EbookQueryResp.class);
        return ebookQueryResps;
    }

    @Override
    public void save(EbookSaveReq req) {
        Ebook ebook = CopyUtil.copy(req, Ebook.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            // 新增
            ebook.setId(snowFlake.nextId());
            ebookMapper.insert(ebook);
        } else {
            // 更新
            ebookMapper.updateByPrimaryKey(ebook);
        }
    }

    @Override
    public void delete(Long id) {
        ebookMapper.deleteByPrimaryKey(id);
    }

}
